package com.juqimiao.raccoon.commodity.command.query;

import com.juqimiao.linn.command.CommandExecutor;
import com.juqimiao.linn.command.CommandExecutorI;
import com.juqimiao.raccoon.commodity.appobj.CategoryGroupListAO;
import com.juqimiao.raccoon.commodity.convertor.CategoryGroupConverter;
import com.juqimiao.raccoon.commodity.tunnel.database.CategoryGroupTunnelI;
import com.juqimiao.raccoon.commodity.tunnel.database.dataobject.CategoryGroupDO;
import com.juqimiao.raccoon.commodity.dto.query.ListCategoryGroupQry;
import com.juqimiao.raccoon.commodity.extension.CategoryGroupValidExtPt;
import com.juqimiao.linn.extension.ExtensionExecutor;
import com.juqimiao.linn.transfer.query.QueryResponse;
import com.juqimiao.linn.transfer.query.builders.QueryResponseBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@CommandExecutor
public class ListCategoryGroupQryExt implements
        CommandExecutorI<QueryResponse<List<CategoryGroupListAO>>, ListCategoryGroupQry> {

    @Autowired
    private CategoryGroupTunnelI categoryGroupTunnel;
    @Autowired
    private ExtensionExecutor extensionExecutor;
    @Autowired
    private CategoryGroupConverter categoryGroupConverter;

    @Override
    public QueryResponse<List<CategoryGroupListAO>> execute(ListCategoryGroupQry query) {
        extensionExecutor.executeVoid(CategoryGroupValidExtPt.class,
                query.getBusinessPoint(),
                extension -> extension.validate(query));

        List<CategoryGroupListAO> categoryGroupListAOList = new ArrayList<>();
        List<CategoryGroupDO> categoryGroupDOList = categoryGroupTunnel.list(query.getGroupType(),
                query.getScenario(),
                query.getPublish(),
                query.getSortOrderSortCrt().getOrderType().toString());
        categoryGroupDOList.forEach(categoryGroupdo -> {
            categoryGroupListAOList.add(categoryGroupConverter.dataObjectToAppObject(categoryGroupdo));
        });

        return QueryResponseBuilder.builder()
                .success("success")
                .of(categoryGroupListAOList).build();
    }
}
